//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.dao
{
	import __AS3__.vec.Vector;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	
	import reader.model.vo.BlogLabelVO;
	import reader.model.vo.UserVO;

	public class LabelDAO extends BaseDAO implements ILabelDAO
	{
		//////////////////////////////////////////////////////////////////////////////
		//SQLStatement queries
		
		private static const INSERT_LABEL:String = "INSERT INTO labels (name, userId) VALUES(:name, :userId)";
		private static const UPDATE_LABEL:String = "UPDATE labels SET name=:name WHERE labelid=:labelid";
		private static const DELETE_LABEL:String = "DELETE FROM labels WHERE labelid=:labelid";
		private static const DELETE_LABEL_BY_USER:String = "DELETE FROM labels WHERE userId=:userId";
		private static const SELETE_LABEL:String = "SELECT * FROM labels WHERE labelid=:labelid";
		private static const SELETE_ALL_LABEL:String = "SELECT * FROM labels WHERE userId=:userId";
		private static const SELECT_COUNT:String = "SELECT COUNT(labelid) FROM labels WHERE userId=:userId";
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function LabelDAO(connection:SQLConnection)
		{
			super(connection);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		//----------------------------------------------------------------------------
		//
		// INSERT
		//
		//----------------------------------------------------------------------------
		
		public function insertLabel(label:BlogLabelVO, user:UserVO):int
		{
			var sqlResult:SQLResult = execute(INSERT_LABEL, {name: label.name, userId: user.userId});
            return int(sqlResult.lastInsertRowID);
		}
		
		//----------------------------------------------------------------------------
		//
		// UPDATE
		//
		//----------------------------------------------------------------------------
		
		public function updateLabel(label:BlogLabelVO):Boolean
		{
            return update(UPDATE_LABEL, {name: label.name, labelid: label.labelid});
		}
		
		//----------------------------------------------------------------------------
		//
		// DELETE
		//
		//----------------------------------------------------------------------------
		
		public function deleteLabel(label:BlogLabelVO):Boolean
		{
            return drop(DELETE_LABEL, {labelid: label.labelid});
		}
		
		public function deleteLabelByUser(user:UserVO):Boolean
		{
            return drop(DELETE_LABEL_BY_USER, {userId: user.userId});
		}
		
		public function deleteLabels(labels:Vector.<BlogLabelVO>):void
		{
            this.connection.begin();
            
            for each(var lb:BlogLabelVO in labels)
            {
            	executeTransaction(DELETE_LABEL, {labelid: lb.labelid});
            }
            
            try
            {
            	this.connection.commit();
            }
            catch(e:Error)
            {
            	trace(e);
            }
		}
		
		//----------------------------------------------------------------------------
		//
		// SELETE
		//
		//----------------------------------------------------------------------------
		
		public function selectLabel(labelId:int):BlogLabelVO
		{
			return select(SELETE_LABEL, {labelid: labelId})[0];
		}
		
		public function selectAllLabels(user:UserVO):Vector.<BlogLabelVO>
		{
			return select(SELETE_ALL_LABEL, {userId: user.userId});
		}
		
		private function select(query:String, data:Object):Vector.<BlogLabelVO>
		{
			var result:SQLResult = execute(query, data);
			if(!result || !result.data)
			{
				return null;
			}
			
			var dataArray:Vector.<BlogLabelVO> = new Vector.<BlogLabelVO>();
						
			for each(var obj:Object in result.data)
			{
				var _data:BlogLabelVO = new BlogLabelVO();
				_data.labelid = obj["labelid"];
				_data.name = obj["name"];
				
				dataArray.push(_data);
			}
            return dataArray;
		}
		
		
		//----------------------------------------------------------------------------
		//
		// COUNT
		//
		//----------------------------------------------------------------------------
		
		public function getCount(user:UserVO):uint
		{
			return count(SELECT_COUNT, {userId: user.userId});
		}
		
	}
}